package com.miracle.system.web;

import com.miracle.common.utils.SearchUtils;
import com.miracle.common.utils.SecurityUtils;
import com.miracle.common.utils.StringUtils;
import com.miracle.system.base.domain.ResultMap;
import com.miracle.system.domain.SysConfig;
import com.miracle.system.domain.SysUser;
import com.miracle.system.service.SysConfigService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：miracle.cui
 * @description：TODO
 * @date ：2020/9/25 10:23
 */
@ApiModel(value = "配置管理类")
@RestController
@RequestMapping("/system/config")
public class SysConfigController {

    @Autowired
    SysConfigService configService;

    @GetMapping("/list")
    @ApiOperation(value = "配置信息列表")
    public ResultMap list(SysConfig config){
        SearchUtils.startPage();
        List<SysConfig> configList = configService.selectConfigList(config);
        return ResultMap.getPage(configList);
    }

    @ApiOperation(value = "新增配置信息")
    @PostMapping
    public ResultMap add(@Validated @RequestBody SysConfig config){
        config.setCreateBy(SecurityUtils.getUserId());
        Integer rows = configService.insertConfig(config);
        return ResultMap.success();
    }

    @ApiOperation(value = "删除配置信息")
    @DeleteMapping
    public ResultMap delete(@Validated @RequestBody Integer[] configIds){

        Integer rows = configService.deleteConfigByConfigIds(configIds);
        return rows > 0 ? ResultMap.success() : ResultMap.error();
    }

    @ApiOperation(value = "修改配置信息")
    @PutMapping
    public ResultMap edit(@Validated @RequestBody SysConfig config){
        config.setLastUpdateBy(SecurityUtils.getUserId());
        Integer rows = configService.updateConfig(config);
        return rows > 0 ? ResultMap.success(config) : ResultMap.error();
    }

    @ApiOperation(value = "查询配置信息")
    @GetMapping(value = {"/", "/{configId}"})
    public ResultMap getInfo(@PathVariable(value = "configId", required = false) Integer configId){

        if (StringUtils.isNotNull(configId)){
            return ResultMap.success(configService.selectConfigByConfigId(configId));
        } else {
            return ResultMap.error();
        }
    }
    /* 远程校验 */
    @ApiOperation(value = "远程校验配置信息")
    @GetMapping(value = {"/remoteCheckConfig"})
    public ResultMap remoteCheckConfig(SysConfig config){
        config.setStatus("enabled");
        SysConfig checkConfig = configService.remoteCheckConfig(config);
        return ResultMap.success(checkConfig);
    }
}
